<?php

declare(strict_types=1);

namespace app\service\platform;

use app\model\platform\AgreementModel;
use app\model\platform\FeedbackModel;
use owns\auth\UserAuth;

class PlatformService
{
    /**
     * 协议或文章列表
     * @param array $param
     */
    public function agreementList(array $param): array
    {
        $list = AgreementModel::custom($param)
            ->order('sort asc, id desc')
            ->pages($param)
            ->select();
        return list_fmt($list, AgreementModel::custom($param)->count());
    }
    /**
     * 协议或文章编辑
     * @param array $data
     * @param integer $id
     */
    public function agreementEdit(array $data, int $id)
    {
        $auth = UserAuth::instance();
        if ($id) {
            $agreement = AgreementModel::find($id);
            if (!$agreement) {
                tips('未找到相关信息记录，修改失败');
            }
            $agreement->updated_by = $auth->username();
        } else {
            $agreement = new AgreementModel();
            $agreement->created_by = $auth->username();
        }
        $agreement->name = trim($data['name']);
        $agreement->mark = trim($data['mark']);
        $agreement->content = $data['content'];
        $agreement->sort = $data['sort'];
        return $agreement->save();
    }
    /**
     * 客户反馈列表
     * @param array $param
     */
    public function feedbackList(array $param): array
    {
        $list = FeedbackModel::custom($param)
            ->order('is_read asc,id desc')
            ->pages($param)
            ->select();
        return list_fmt($list, FeedbackModel::custom($param)->count());
    }
    /**
     * 客户反馈详情
     * @param integer $id
     */
    public function feedbackInfo(int $id)
    {
        $info = FeedbackModel::find($id);
        if ($info) {
            $info->enclosures = explode(',', $info->enclosures);
            if ($info->is_read == 0) {
                $info->is_read = 1;
                $info->save();
            }
        }
        return $info;
    }
}
